//-------------------------------------------------------------------------------------------------->
//
// Copyright 2002 e Professional Corporation.  All Rights Reserved.
//
// File:         	CAEPMU08.java
//
// Author:   	poompui
//
// Date:			02/05/2003
//
//--------------------------------------------------------------------------------------------------->
package com.epro.standard.frontend.servlet.masterutil;

//======================Import package=======================>
import java.io.IOException;

import java.sql.Timestamp;

import java.util.Date;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epro.standard.StandardException;
import com.epro.standard.frontend.datasetup.MessageManager;
import com.epro.standard.frontend.servlet.EproServlet;
import com.epro.standard.frontend.util.EproServletUtil;
import com.epro.standard.frontend.util.FileInfoUtil;
import com.epro.standard.frontend.util.StringUtil;
import com.epro.standard.usermenu.RoleMenuEntity;
import com.epro.standard.usermenu.RoleMenu_MenuVO;
import com.epro.standard.usermenu.UserAccountVO;
import com.epro.standard.usermenu.manage.MenuRoleManage;

public class CAEPMU08 extends EproServlet {
    public void doPost(HttpServletRequest request, 
                       HttpServletResponse response) throws ServletException, 
                                                            IOException {
        //======================Define Variable======================>
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);
        String action = (servletUtil.getRequestValue("action")).toLowerCase();

        //=================Select action for Managing==================>		
        if (action.equals("insert"))
            insertData(request, response);
        else if (action.equals("update"))
            updateData(request, response);
        else if (action.equals("delete"))
            deleteData(request, response);
    }


    //-------------------------------------------------------------------------------------------------->
    //																																	\\
    //													INSERT																\\
    //																																	\\
    //--------------------------------------------------------------------------------------------------->	

    public void insertData(HttpServletRequest request, 
                           HttpServletResponse response) throws ServletException, 
                                                                IOException {
        //======================Define Variable======================>
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);
        //   String userLID= ((com.epro.mol.usermanage.UserProfile)session.getAttribute("userLogin")).getUserID();
        String userLID = "dosent login";
        String roleID = servletUtil.getRequestValue("roleID");
        String menuID = servletUtil.getRequestValue("menuID");

        //======================Define redirect page==================>
        String parameterLink = "&roleID=" + roleID;
        String URL = 
            ((FileInfoUtil)session.getAttribute("fileInfoUtil")).getRedirectFromServlet();
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;

        //======================Define Object======================>
        MenuRoleManage userManager = new MenuRoleManage();

        try {
            String delimeter = ",";
            java.util.StringTokenizer stMenuID = 
                new java.util.StringTokenizer(menuID, delimeter);
            while (stMenuID.hasMoreTokens()) {
                String tmpMenuID = stMenuID.nextToken();
                RoleMenuEntity entity = new RoleMenuEntity(roleID, tmpMenuID);
                entity.setAbleDelete("Y");
                entity.setAbleInsert("Y");
                entity.setAbleUpdate("Y");
                entity.setAbleExecute("Y");
                userManager.addMenuByRole(entity);
            }
            session.setAttribute("hasError", 
                                 MessageManager.getInsertSuccessString());
            response.sendRedirect(successLink);
        } catch (StandardException e) {
            e.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getInsertUnsuccessString() + 
                                 StringUtil.encode2Thai(e.getDescription()));
            response.sendRedirect(unsuccessLink);
        }
    }


    //-------------------------------------------------------------------------------------------------->
    //																																	\\
    //													UPDATE																\\
    //																																	\\
    //--------------------------------------------------------------------------------------------------->	

    public void updateData(HttpServletRequest request, 
                           HttpServletResponse response) throws ServletException, 
                                                                IOException {
        //======================Define Variable======================>
        HttpSession session = request.getSession();
        EproServletUtil servletUtil = new EproServletUtil(request);

        String userUpd = 
            session.getAttribute("userLogin") == null ? "Doesn't Signin" : 
            ((UserAccountVO)session.getAttribute("userLogin")).getUA_USER_NAME();
        int index = servletUtil.getRequestIntValue("index");
        String dataIsChange[] = servletUtil.getRequestValues("dataIsChange");

        String execute[] = servletUtil.getRequestValues("originalExecute");
        String originalExecute[] = 
            servletUtil.getRequestValues("originalExecute");
        String masterExecute[] = 
            servletUtil.getRequestValues("executeIsChange");

        String insert[] = servletUtil.getRequestValues("originalInsert");
        String originalInsert[] = 
            servletUtil.getRequestValues("originalInsert");
        String masterInsert[] = servletUtil.getRequestValues("insertIsChange");

        String update[] = servletUtil.getRequestValues("originalUpdate");
        String originalUpdate[] = 
            servletUtil.getRequestValues("originalUpdate");
        String masterUpdate[] = servletUtil.getRequestValues("updateIsChange");

        String delete[] = servletUtil.getRequestValues("originalDelete");
        String originalDelete[] = 
            servletUtil.getRequestValues("originalDelete");
        String masterDelete[] = servletUtil.getRequestValues("deleteIsChange");
        String roleID = servletUtil.getRequestValue("roleID");

        //======================Define redirect page==================>
        String parameterLink = "&roleID=" + roleID;
        String URL = 
            ((FileInfoUtil)session.getAttribute("fileInfoUtil")).getRedirectFromServlet();
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;

        //======================Define Object======================>
        MenuRoleManage manager = new MenuRoleManage();

        try {
            boolean updateData = false;
            if (dataIsChange == null) {
                session.setAttribute("hasError", 
                                     MessageManager.getUnChangeDataString());
                response.sendRedirect(unsuccessLink);
            } else {
                for (int i = 0; i < dataIsChange.length; i++) {
                    if (masterExecute[i].equals("N"))
                        execute[i] = originalExecute[i];
                    else
                        execute[i] = 
                                originalExecute[i].equals("Y") ? "N" : "Y";
                    if (masterInsert[i].equals("N"))
                        insert[i] = originalInsert[i];
                    else
                        insert[i] = originalInsert[i].equals("Y") ? "N" : "Y";
                    if (masterUpdate[i].equals("N"))
                        update[i] = originalUpdate[i];
                    else
                        update[i] = originalUpdate[i].equals("Y") ? "N" : "Y";
                    if (masterDelete[i].equals("N"))
                        delete[i] = originalDelete[i];
                    else
                        delete[i] = originalDelete[i].equals("Y") ? "N" : "Y";
                }

                String ar[];
                ar = new String[dataIsChange.length];
                ar = dataIsChange;
                Vector dataVector = 
                    (Vector)session.getAttribute("RoleMenuManagerVector");
                boolean chkSubmit = false;
                int numE = 0, numD = 0;
                String canUpdate = "";
                String cannotUpdate = "";

                int preData = servletUtil.getRequestIntValue("preData");
                for (int i = 0; i < ar.length; i++) {
                    if (dataIsChange[i].equals("Y")) {
                        RoleMenu_MenuVO VO = 
                            (RoleMenu_MenuVO)dataVector.elementAt(i + preData);
                        RoleMenuEntity entity = 
                            new RoleMenuEntity(VO.getRM_ROLE_ID(), 
                                               VO.getRM_MENU_ID());
                        Date date = new Date();
                        Timestamp timestamp = new Timestamp(date.getTime());
                        entity.setAbleExecute(execute[i]);
                        entity.setAbleInsert(insert[i]);
                        entity.setAbleUpdate(update[i]);
                        entity.setAbleDelete(delete[i]);
                        manager.editMenuByRole(entity);
                        numD = numD + 1;
                        chkSubmit = true;
                    }
                }

                if (numD != 0)
                    session.setAttribute("hasError", 
                                         MessageManager.getUpdateSuccessAllString() + 
                                         numD + 
                                         MessageManager.getUnitString());

                if (chkSubmit)
                    response.sendRedirect(successLink);
                else {
                    session.setAttribute("hasError", 
                                         MessageManager.getUnChangeDataString());
                    response.sendRedirect(unsuccessLink);
                }
            } // end if( dataIsChange.length == 0 )

        } catch (StandardException e) {
            e.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getUpdateUnsuccessString() + 
                                 StringUtil.encode2Thai(e.getDescription()));
            response.sendRedirect(unsuccessLink);
        }
    }


    //-------------------------------------------------------------------------------------------------->
    //																																	\\
    //													DELETE																\\
    //																																	\\
    //--------------------------------------------------------------------------------------------------->	

    public void deleteData(HttpServletRequest request, 
                           HttpServletResponse response) throws ServletException, 
                                                                IOException {
        //======================Define Variable======================>
        HttpSession session = request.getSession();
        EproServletUtil servletUtil = new EproServletUtil(request);

        String userUpd = 
            session.getAttribute("userLogin") == null ? "Doesn't Signin" : 
            ((UserAccountVO)session.getAttribute("userLogin")).getUA_USER_ID();
        int index = servletUtil.getRequestIntValue("index");
        String chkRow[] = servletUtil.getRequestValues("chkRow");
        String roleID = servletUtil.getRequestValue("roleID");

        //======================Define redirect page==================>
        String parameterLink = "&roleID=" + roleID;
        String URL = 
            ((FileInfoUtil)session.getAttribute("fileInfoUtil")).getRedirectFromServlet();
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;

        //======================Define Object======================>
        MenuRoleManage manager = new MenuRoleManage();

        try {
            if (chkRow == null) {
                session.setAttribute("hasError", 
                                     MessageManager.getDeleteUnSelectString());
                response.sendRedirect(unsuccessLink);
            } else {
                String ar[];
                ar = new String[chkRow.length];
                ar = chkRow;
                Vector dataVector = 
                    (Vector)session.getAttribute("RoleMenuManagerVector");
                boolean chkSubmit = false;
                int numE = 0, numD = 0;
                String canUpdate = "";
                String cannotUpdate = "";

                for (int i = 0; i < ar.length; i++) {
                    RoleMenu_MenuVO VO = 
                        (RoleMenu_MenuVO)dataVector.get(Integer.parseInt(ar[i]) + 
                                                        index);
                    RoleMenuEntity entity = 
                        new RoleMenuEntity(VO.getRM_ROLE_ID(), 
                                           VO.getRM_MENU_ID());
                    Date date = new Date();
                    Timestamp timestamp = new Timestamp(date.getTime());
                    manager.removeMenuByRole(entity);
                    numD = numD + 1;
                    chkSubmit = true;
                }

                if (numD != 0)
                    session.setAttribute("hasError", 
                                         MessageManager.getDeleteSuccessALLString() + 
                                         numD + 
                                         MessageManager.getUnitString());

                if (chkSubmit)
                    response.sendRedirect(successLink);
                else {
                    session.setAttribute("hasError", 
                                         MessageManager.getDeleteUnsuccessString());
                    response.sendRedirect(unsuccessLink);
                }
            } // end if( chkRow.length == 0 )
        } catch (StandardException e) {
            e.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getDeleteUnsuccessString() + 
                                 StringUtil.encode2Thai(e.getDescription()));
            response.sendRedirect(unsuccessLink);
        }
    }
}

